package com.welsy.classroom.command
{
	import com.adobe.cairngorm.commands.Command;
	import mx.rpc.IResponder;
	import com.welsy.classroom.event.GetClassComponentsEvent;
	import com.welsy.classroom.business.GetClassComponentsDelegate;
	import com.welsy.usermgt.vo.ClassVO;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.usermgt.vo.UserVO;
	import com.welsy.classroom.model.ClassModelLocator;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import com.welsy.classroom.business.GetAllEditingClassroomDelegate;
	import com.welsy.classroom.model.ClassModelLocator;
	import mx.collections.ArrayCollection;
	import com.welsy.iter.business.IterQNames;
	import com.welsy.iter.vo.IterVO;
	import com.welsy.classroom.model.ClassModelLocator;
	
	public class GetClassComponentsCommand implements Command, IResponder
	{
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetClassComponentsDelegate = new GetClassComponentsDelegate(this);
			var getClassComponentsEvent : GetClassComponentsEvent = GetClassComponentsEvent(event);
			
			delegate.getComponents(getClassComponentsEvent.classId);
		}
		
		public function result(event : Object) :void {
			//parse the server result
			var aNS : Namespace = new Namespace(NamespaceLocator.USER_MGT_DATATYPES);
			var bNS : Namespace = new Namespace(NamespaceLocator.CLASSROOM_DATATYPES);
			var base : XMLList = XMLList(event.result);
			
			var TAG_USERSCOLLECTION : QName = new QName(bNS, "UsersCollection");
			var TAG_USERTYPE : QName = new QName(aNS, "UserType");
			var TAG_USERID : QName = new QName(aNS, "UserId");
			var TAG_SUBSYSTEMID : QName = new QName(aNS, "SubSystemId");
			var TAG_FIRSTNAME : QName = new QName(aNS, "FirstName");
			var TAG_LASTNAME : QName = new QName(aNS, "LastName");
			var TAG_LOGIN : QName = new QName(aNS, "Login");
			var TAG_EMAIL : QName = new QName(aNS, "Email");
			var TAG_ROLEID : QName = new QName(aNS, "RoleId");
			
			var classVO : ClassVO = ClassModelLocator.getInstance().currentClass;
			
			//add the users
			var users : XMLList = base.descendants(TAG_USERSCOLLECTION).children();
			classVO.classUsers.removeAll();
			for each(var user : XML in users) {
				var buffer : UserVO = new UserVO;
				buffer.UserId = user.child(TAG_USERID);
				buffer.FirstName = user.child(TAG_FIRSTNAME);
				buffer.LastName= user.child(TAG_LASTNAME);
				buffer.Login = user.child(TAG_LOGIN);
				buffer.Email = user.child(TAG_EMAIL);
				buffer.RoleId = user.child(TAG_ROLEID);
				buffer.SubSystemId = user.child(TAG_SUBSYSTEMID);
				
				classVO.classUsers.addItem(buffer);
			}
			var TAG_ITERSCOLLECTION : QName = new QName(NamespaceLocator.CLASSROOM_DATATYPES, "ItersCollection");
			
			var iters : XMLList = base.descendants(TAG_ITERSCOLLECTION).children();
			ClassModelLocator.getInstance().relatedItinera = parseIter(iters);
			
		}
		
		public function fault( event : Object ):void {
			//TODO: report the error
			Alert.show("GetClassComponentsCommand.fault!");
		}
		
		private function parseIter(list : XMLList) : ArrayCollection {
			var iters : ArrayCollection = new ArrayCollection();
			var item:XML;
			trace("Parse: "+list.toXMLString());
			for each(item in list) { 				         	
   	     		var iterVO:IterVO = new IterVO;
				iterVO.setProperties(item);
				iters.addItem(iterVO);            	            	
	 		}
   			return iters;
		}
	}
}